const signature = require("./signature.js");
const request = require("request");

const deviceinfo =
  "CH=1&Height=772&Lang=zh_CN&Model=microsoft&Net=unknown&PID=1&PixelRatio=1&Ver=2&Version=3.9.12&Width=398";

module.exports = {
  _before: function () {
    // 通用预处理器
  },
  // 获取banner列表
  // SignatureStamp
  async getInit() {
    const res = await uniCloud.request({
      url: "https://minibizhi.313515.com/List/SelectionList",
      method: "GET",
      header: {
        deviceinfo: signature(deviceinfo),
      },
    });
    if (res.statusCode === 200) {
      const data = res.data.Data;
      const params = {
        banner: data.CarouselArray,
        Albums: data.AlbumArray,
        navigations: data.NavigationList,
        data,
      };
      return request(200, params);
    } else {
      return request(400, "服务器异常");
    }
  },

  // 根据分类,获取图片列表
  async getImages(item) {
    const res = await uniCloud.request({
      url: `https://minibizhi.313515.com/List/WallpaperList?${signature(item)}`,
      method: "GET",
      header: {
        deviceinfo: signature(deviceinfo),
      },
    });
    if (res.statusCode === 200) {
      return request(200, res.data.Data.DataList);
    } else {
      return request(400, "服务器异常");
    }
  },
  // 获取专辑类目列表
  async getAlbumTypeList() {
    const res = await uniCloud.request({
      url: "https://minibizhi.313515.com/List/AlbumList",
      method: "GET",
      header: {
        deviceinfo: signature(deviceinfo),
      },
    });
    if (res.statusCode === 200) {
      const filters = [10006, 25];
      return request(200, {
        tabsList: res.data.Data.CateList.filter(
          (item) => !filters.includes(item.CategoryID)
        ),
        dataList: res.data.Data.DataList,
      });
    } else {
      return request(400, "服务器异常");
    }
  },
  // 获取专辑列表
  async getAlbumList(item) {
    const res = await uniCloud.request({
      url: `https://minibizhi.313515.com/List/AlbumList?${signature(item)}`,
      method: "GET",
      header: {
        deviceinfo: signature(deviceinfo),
      },
    });
    if (res.statusCode === 200) {
      return request(200, res.data.Data.DataList || []);
    } else {
      return request(400, "服务器异常");
    }
  },
  // 获取专辑详情
  async getAlbumDetails(item) {
    const res = await uniCloud.request({
      url: `https://minibizhi.313515.com/list/AlbumContent?${signature(item)}`,
      method: "GET",
      header: {
        deviceinfo: signature(deviceinfo),
      },
    });
    if (res.statusCode === 200) {
      return request(200, {
        album: res.data.Data.Album,
        dataList: res.data.Data.DataList,
      });
    } else {
      return request(400, "服务器异常");
    }
  },
  // 获取头像列表
  async getAvatarList(pageNum) {
    const res = await uniCloud.request({
      url: `https://mengdong-admin.jintianwang.cc/api/wxaopen/draft/getHistoryDraftListByMidId?merchant_id=147&pageNum=${pageNum}&draft_type=2`,
      method: "GET",
      header: {
        businessid: 1,
      },
    });

    if (res.statusCode === 200) {
      const arr = res.data.data.records
        .map((item) => item.image_list.slice(0, -1))
        .flat(Infinity);
      return request(200, arr);
    } else {
      return request(400, "服务器异常");
    }
  },
};
